package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.PrizeDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PrizeMapper {

    @Insert("insert into prize (name, image_url, price, description) " +
            "values(#{name}, #{imageUrl}, #{price},#{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(PrizeDO prizeDO);

    @Select("<script> " +
            "select count(1) from prize " +
            " <if test=\"prizeName!=null\">" +
            "where name like concat('%',#{prizeName},'%') " +
            " </if>" +
            "</script>")
    int count(@Param("prizeName") String prizeName);

    @Select("<script>  " +
            "select * from prize " +
            "<if test =\"prizeName != null\"> " +
            "where name like concat('%',#{prizeName},'%') " +
            "</if>" +
            "order by id desc limit #{offset}, #{pageSize} " +
            "</script>")
    List<PrizeDO> selectPrizeList(@Param("offset") Integer offset, @Param("pageSize") Integer pageSize, @Param("prizeName") String prizeName);

    @Select("<script>" +
            " select id from prize " +
            " where id in " +
            "   <foreach item='item' collection='items' open='(' separator =',' close=')'> " +
            "       #{item}" +
            "   </foreach>" +
            "</script>")
    List<Long> selectExistsByIds(@Param("items") List<Long> ids);

    @Select("<script>" +
            " select * from prize " +
            " where id in " +
            "   <foreach item='item' collection='items' open='(' separator =',' close=')'> " +
            "       #{item}" +
            "   </foreach>" +
            "</script>")
    List<PrizeDO> batchSelectByIds(@Param("items") List<Long> ids);

    @Select("select * from prize where id = #{id}")
    PrizeDO selectById(@Param("id") Long prizeId);

    @Delete("delete from prize where id = #{id}")
    Integer deleteByPrizeId(@Param("id") Long prizeId);

    @Update("<script> " +
            "update  prize set name = #{name}, description = #{description}, price = #{price} " +
            "<if test =\"imageUrl != null\"> " +
            ", image_url = #{imageUrl} " +
            "</if>" +
            "where id = #{id} " +
            "</script>")
    Integer update(PrizeDO prizeDO);
}
